package com.atguigu.lease.app.service.impl;

import com.atguigu.lease.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.app.service.*;
import com.atguigu.lease.common.handler.StgyAppAuthInterceptor;
import com.atguigu.lease.model.bean.GraphInfo;
import com.atguigu.lease.model.bean.LeaseAgreement;
import com.atguigu.lease.model.bean.LeaseTerm;
import com.atguigu.lease.model.bean.UserInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.atguigu.lease.model.vo.apartment.GraphVo;
import com.atguigu.lease.model.vo.app.AgreementDetailVo;
import com.atguigu.lease.model.vo.app.AgreementItemVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YouKinn
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2025-06-20 20:18:17
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    LeaseTermService leaseTermService;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    RoomInfoService roomInfoService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    PaymentTypeService paymentTypeService;

    @Override
    public List<AgreementItemVo> listItem() {
        //1、先查询用户的手机号
        UserInfo userInfo = userInfoService
                .getById(StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get().getId());
        //2、查询数据
        return baseMapper.selectAgreementItemVoListItem(userInfo.getPhone());
    }

    @Override
    public void saveOrUpdateLeaseAgreement(LeaseAgreement leaseAgreement) {
        //根据租期 计算 租约到期时间
        boolean isUpdate = leaseAgreement.getId() != null;
        //查询 租期信息：包含了月数
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        Date startDate = new Date();
        Date endDate = new Date();
        if (isUpdate) {
            //更新：
            startDate = leaseAgreement.getLeaseStartDate();
            //1、如果租约未到期： 应该在截止日期上+  续约的月数
            if (leaseAgreement.getLeaseEndDate().after(endDate)) {
                //租约未到期 进行的续约
                //使用 未到期的时间日期+ 租期的月数
                endDate = new DateTime(leaseAgreement.getLeaseEndDate())
                        .plusMonths(leaseTerm.getMonthCount()).toDate();
            } else {
                //2、如果租约已到期： 应该在当前日期上+ 续约的月数
                endDate = new DateTime() //当前日期+续约的月数
                        .plusMonths(leaseTerm.getMonthCount()).toDate();
            }
        } else {
            //3、如果是新的租约： 应该在当前日期上+ 续约的月数
            endDate = new DateTime() //当前日期+续约的月数
                    .plusMonths(leaseTerm.getMonthCount()).toDate();
        }

        leaseAgreement.setLeaseStartDate(startDate);
        leaseAgreement.setLeaseEndDate(endDate);

        this.saveOrUpdate(leaseAgreement);

    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {

        AgreementDetailVo vo = new AgreementDetailVo();

        LeaseAgreement leaseAgreement = this.getById(id);
        BeanUtils.copyProperties(leaseAgreement, vo);

        String apartmentName = apartmentInfoService
                .getById(leaseAgreement.getApartmentId()).getName();
        vo.setApartmentName(apartmentName);

        String roomNumber = roomInfoService.getById(leaseAgreement.getRoomId()).getRoomNumber();
        vo.setRoomNumber(roomNumber);

        List<GraphVo> apartmentGraphVos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getItemId, leaseAgreement.getApartmentId())
                        .eq(GraphInfo::getItemType, ItemTypeEnum.APARTMENT))
                .stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }).collect(Collectors.toList());
        vo.setApartmentGraphVoList(apartmentGraphVos);

        List<GraphVo> roomGraphVos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getItemId, leaseAgreement.getRoomId())
                        .eq(GraphInfo::getItemType, ItemTypeEnum.ROOM))
                .stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }).collect(Collectors.toList());
        vo.setRoomGraphVoList(roomGraphVos);

        String paymentTypeName = paymentTypeService.getById(leaseAgreement.getPaymentTypeId()).getName();
        vo.setPaymentTypeName(paymentTypeName);

        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        vo.setLeaseTermUnit(leaseTerm.getUnit());
        vo.setLeaseTermMonthCount(leaseTerm.getUnit());
        return vo;
    }
}





